(function(Global /*, context */) {
    var playerInfoData=null
    var playerInfoELE=$("#PlayerInfo")
    var toggleState;


    function SetData(data) {
        // $.Msg("player_info  ",data)
        playerInfoData=data
        var children=playerInfoELE.Children()
        // $.Msg(children)
        for(var i in data){
           var item = playerInfoELE.FindChildTraverse("item"+data[i].playerId);
           if(item===null) {
               item =$.CreatePanel( "Panel", playerInfoELE, "item"+data[i].playerId );
               item.BLoadLayout( "file://{resources}/layout/custom_game/player_info_item.xml", false, false );
           }
            item.SetItemData(data[i])
            if(children!==null){
                playerInfoELE.MoveChildBefore( item,  playerInfoELE.GetChild((Number(i)-1)));
            }
        }

    }



    function OnToggle(event){
        if (event.isOpen===toggleState){
            return
        }
        if(playerInfoData!==null){
            for(var i in playerInfoData){
                var item = playerInfoELE.FindChildTraverse("item"+playerInfoData[i].playerId);
                if(item!==null) {
                    item.Toggle(event.isOpen);
                }
            }
            toggleState=event.isOpen;
        }

    }


    GameEvents.Subscribe( "toggle_player_info", OnToggle );

    $.GetContextPanel().SetData=SetData;
    $.GetContextPanel().OnToggle=OnToggle;

    Global.Panel.playerInfo=$.GetContextPanel()

})(GameUI.CustomUIConfig(), this);